package blatt4;

//
//CannonGame application
//Described in Chapter 6 of
//Understanding Object - Oriented Programming with Java
//by Timothy A Budd
//Published by Addison - Wesley
//
//see ftp://ftp.cs.orst.edu/pub/budd/java/ReadMe.html
//for further information
//
//reformatted and slightly modified. peter, oct 2000
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

class CannonGame extends Frame implements KeyListener {

	public static void main(String[] args) {
		CannonGame world;

		if (args[0] == null) {
			world = new CannonGame(0);
		} else {
			world = new CannonGame(Integer.parseInt(args[0]));
		}

		world.show();
		// Close window
		world.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
	}

	public static final int FrameWidth = 600;
	public static final int FrameHeight = 400;
	private int angle = 0;
	private String message = "";
	private CannonBall cannonBall = null;
	private static final int barrelLength = 30;
	private static final int barrelWidth = 10;

	public CannonGame(int theta) { 
		// resize our frame
		setSize(FrameWidth, FrameHeight);
		setTitle("Cannon Game");
	    addKeyListener(this);

		// set the local variables
		angle = theta;
		message = "Angle = " + angle;
		
		cannonBall = createBall(angle);
	}
	
	public CannonBall createBall(int angle){
		// create the cannon ball
		double radianAngle = angle * Math.PI / 180.0;
		double sinAngle = Math.sin(radianAngle);
		double cosAngle = Math.cos(radianAngle);

		// create the cannon ball
		return new CannonBall(20 + (int) (barrelLength * cosAngle),
				dy(5 + (int) (barrelLength * sinAngle)), 5, 12 * cosAngle, -12
						* sinAngle);		
	}

	public static int dy(int y) {
		return FrameHeight - y;
	}

	public void paint(Graphics g) {
		int x = 20;
		// location of cannon
		int y = 5;
		double radianAngle = angle * Math.PI / 180.0;
		int lv = (int) (barrelLength * Math.sin(radianAngle));
		int lh = (int) (barrelLength * Math.cos(radianAngle));
		int sv = (int) (barrelWidth * Math.sin(radianAngle + Math.PI / 2));
		int sh = (int) (barrelWidth * Math.cos(radianAngle + Math.PI / 2));

		// draw cannon
		g.setColor(Color.green);
		g.drawLine(x, dy(y), x + lh, dy(y + lv));
		g.drawLine(x + lh, dy(y + lv), x + lh + sh, dy(y + lv + sv));
		g.drawLine(x + lh + sh, dy(y + lv + sv), x + sh, dy(y + sv));
		g.drawLine(x + sh, dy(y + sv), x, dy(y));
		g.drawOval(x - 8, dy(y + 10), 12, 12);
		// draw target
		g.setColor(Color.red);
		g.fillRoundRect(FrameWidth - 100, dy(12), 50, 10, 6, 6);

		// draw cannonball
		if (cannonBall != null) {
			cannonBall.move();
			cannonBall.paint(g);

			try {
				Thread.sleep(20);
			} catch (InterruptedException e) {
			}

			if (dy(cannonBall.y()) > 0)
				repaint();
			else {
				int targetX = FrameWidth - 100;

				if ((cannonBall.x() > targetX)
						&& (cannonBall.x() < (targetX + 50)))
					message = "You Hit It!";
				else
					message = "Missed!";

				cannonBall = null;
			}
		}

		// draw message
		g.drawString(message, FrameWidth / 2, FrameHeight / 2);
	}

	@Override
	public void keyPressed(KeyEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void keyTyped(KeyEvent e) {

		if (e.getKeyChar() != KeyEvent.CHAR_UNDEFINED) {
			char key = e.getKeyChar();

			// Funktionstaste abfragen
			if (key == 'u') {
				angle++;
				message = "Angle = " + angle;
				cannonBall = null;
			} else if (key == 'd') {
				angle--;
				message = "Angle = " + angle;
				cannonBall = null;
			} else if (key == 'f') {
				cannonBall = createBall(angle);
			}
		}
		repaint();

	}
}